<template>
  <!--头部区域-->
  <div style="padding: 10px; height: 20px; line-height: 50px; display: flex">
  </div>
  <div style="padding: 10px">
    <el-dialog v-model="dialogVisible" title="批复详情" width="30%">
      <el-input
          v-model="form.reason"
          style="width: 100%"
          :rows="5"
          type="textarea"
          placeholder="暂无批复"
          disabled
      />
      <template #footer>
        <span>
          <el-button type="primary" color="#fab6b6" @click="dialogVisible = false">
            关闭</el-button
          >
        </span>
      </template>
    </el-dialog>
    <el-table :data="tableData" border="border">
      <el-table-column prop="id" label="申请ID" width="120px" />
      <el-table-column prop="podName" label="Docker名" />
      <el-table-column prop="cpu" label="cpu核心数" />
      <el-table-column prop="memory" label="内存(MB)" />
      <el-table-column prop="imageOS" label="操作系统" :formatter=formatOS />
      <el-table-column prop="imageVersion" label="操作系统版本" :formatter=formatVersion />
      <el-table-column prop="applyTime" label="申请时长(天)"  />
      <el-table-column prop="type" label="申请类型" :formatter=formatType />
      <el-table-column prop="status" label="申请状态" :formatter=formatStatus />
      <el-table-column label="审核批复" width="100px" align="center">
        <template #default="scope">
          <el-button  @click="handleReason(scope.row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin: 10px 0">
      <el-pagination
        v-model:currentPage="currentPage"
        :page-sizes="[5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import request from "../../untils/request";

export default {
  name: "userManage",

  data() {
    return {
      dialogVisible: false,
      currentPage: 1,
      pageSize: 10,
      total: 0,
      tableData: [],
      form: {}, //某行数据
    };
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      request.get("/podRequest/viewMyRequest", {
          params: {
            pageNum: this.currentPage,
            pageSize: this.pageSize,
          },
        })
        .then((res) => {
          this.tableData = res.data.records;
          this.total = res.data.total;
        });
    },
    formatOS(row){
      if (row.imageOS === "ubuntu") {
        return "Ubuntu";
      }
      if (row.imageOS === "centos") {
        return "CentOS";
      }
    },
    formatVersion(row){
      if (row.imageOS === "ubuntu" && row.imageVersion === "C") {
        return "22.04（GCC，G++捆绑包）";
      }
      if (row.imageOS === "ubuntu" && row.imageVersion === "Python") {
        return "22.04（Python捆绑包）";
      }
      return row.imageVersion;
    },
    formatType(row) {
      return row.type === 0 ? "虚拟机申请" : row.type === 1 ? "续租申请" : "未知";
    },
    formatStatus(row) {
      return row.status === 0 ? "申请中" : row.status === 1 ? "已通过" : row.status === 2 ? "已拒绝" : "未知";
    },
    handleReason(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.dialogVisible = true;
    },
    handleSizeChange(pageSize) {
      //改变当前每页的个数触发
      this.pageSize = pageSize;
      this.load();
    },
    handleCurrentChange(pageNum) {
      //改变当前页码触发
      this.currentPage = pageNum;
      this.load();
    },
  },
};
</script>

<style scoped lang="less">
/deep/ .el-input {
  width: 85%;
}

/deep/ .el-dialog {
  background: #fcf9f4;
}

/deep/.el-form-item {
  margin-bottom: 30px;
}
</style>
<style lang="less">
.el-message-box {
  background: #fcf9f4;
}
</style>
